local qixi = fk.CreateSkill {

  name = "joyex__qixi",

  tags = {  },

}



qixi:addEffect("viewas", {
  name = "joyex__qixi",
  anim_type = "control",
  pattern = "dismantlement",
  card_filter = function(self, player, to_select, selected)
    if #selected == 1 then return false end
    return Fk:getCardById(to_select).color == Card.Black
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("dismantlement")
    c.skillName = qixi.name
    c:addSubcard(cards[1])
    return c
  end,
})

qixi:addEffect(fk.EventPhaseStart, {
  name = "#joyex__qixi_trigger",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(qixi.name) and target == player and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local targets = {}
    for _, p in ipairs(player.room.alive_players) do
      if #p:getCardIds("hej") > 0 then
        table.insert(targets,p.id)
      end
    end
    local to = player.room:askForChoosePlayers(player, targets, 1, 1, "#joyex__qixi_trigger-choose", qixi.name, true)
    if #to > 0 then
      event:setCostData(self, to)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = player.room:getPlayerById(event:getCostData(self)[1])
  if not player.dead and not to.dead then
    local card = player.room:askForCardChosen(player, to,"hej", qixi.name)
    player.room:throwCard(card,qixi.name,to,player)
  end
  end,
})

return qixi